package com.sdhs.qualitymanage.service.impl;

import com.sdhs.businessfile.domain.BusinessFile;
import com.sdhs.businessfile.service.IBusinessFileService;
import com.sdhs.businessfile.service.impl.BusinessFileServiceImpl;
import com.sdhs.common.enums.BusinessTypeEnum;
import com.sdhs.common.utils.DateUtils;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.common.utils.StringUtils;
import com.sdhs.common.utils.spring.SpringUtils;
import com.sdhs.qualitymanage.domain.GcQualityManagement;
import com.sdhs.qualitymanage.domain.QualityTreeSelect;
import com.sdhs.qualitymanage.mapper.GcQualityManagementMapper;
import com.sdhs.qualitymanage.service.IGcQualityManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 质量管理Service业务层处理
 *
 * @author add
 * @date 2025-04-11
 */
@Service
public class GcQualityManagementServiceImpl implements IGcQualityManagementService {
    @Autowired
    private GcQualityManagementMapper gcQualityManagementMapper;

    @Autowired
    private BusinessFileServiceImpl businessFileService;
    /**
     * 查询质量管理
     *
     * @param id 质量管理主键
     * @return 质量管理
     */
    @Override
    public GcQualityManagement selectGcQualityManagementById(Long id) {
        GcQualityManagement gcQualityManagement = gcQualityManagementMapper.selectGcQualityManagementById(id);
        List<BusinessFile> businessFiles = businessFileService.listByBusinessTypeAndId(gcQualityManagement.getDocumentType(), String.valueOf(id));
        gcQualityManagement.setFileList(businessFiles);
        return gcQualityManagement;
    }


    /**
     * 查询清单树结构信息
     *
     * @return 清单树信息集合
     */
    @Override
    public List<QualityTreeSelect> selectManageListTreeList(GcQualityManagement gcQualityManagement) {
        List<GcQualityManagement> manageList = selectGcQualityManagementList(gcQualityManagement);
        return buildDeptTreeSelect(manageList);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param manageList 清单列表
     * @return 下拉树结构列表
     */
    private List<QualityTreeSelect> buildDeptTreeSelect(List<GcQualityManagement> manageList) {
        List<GcQualityManagement> deptTrees = buildManageListTree(manageList);
        return deptTrees.stream().map(QualityTreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param manageList 清单列表
     * @return 树结构列表
     */
    public List<GcQualityManagement> buildManageListTree(List<GcQualityManagement> manageList) {
        List<GcQualityManagement> returnList = new ArrayList<>();
        List<String> tempList = manageList.stream().map(GcQualityManagement::getSubheadingsNumber).collect(Collectors.toList());
        for (GcQualityManagement item : manageList) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(item.getParentSubheadings())) {
                recursionFn(manageList, item);
                returnList.add(item);
            }
        }
        if (returnList.isEmpty()) {
            returnList = manageList;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<GcQualityManagement> list, GcQualityManagement t) {
        // 得到子节点列表
        List<GcQualityManagement> childList = getChildList(list, t);
        t.setChildren(childList);
        for (GcQualityManagement tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<GcQualityManagement> getChildList(List<GcQualityManagement> list, GcQualityManagement t) {
        List<GcQualityManagement> tlist = new ArrayList<>();
        for (GcQualityManagement n : list) {
            if (StringUtils.isNotEmpty(n.getParentSubheadings())
                    && n.getParentSubheadings().equals(t.getSubheadingsNumber())) {
                tlist.add(n);
            }
        }
        return tlist;
    }


    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<GcQualityManagement> list, GcQualityManagement t) {
        return !getChildList(list, t).isEmpty();
    }


    /**
     * 查询质量管理列表
     *
     * @param gcQualityManagement 质量管理
     * @return 质量管理
     */
    @Override
    public List<GcQualityManagement> selectGcQualityManagementList(GcQualityManagement gcQualityManagement) {
        return gcQualityManagementMapper.selectGcQualityManagementList(gcQualityManagement);
    }

    /**
     * 新增质量管理
     *
     * @param gcQualityManagement 质量管理
     * @return 结果
     */
    @Override
    public int insertGcQualityManagement(GcQualityManagement gcQualityManagement) {
        return gcQualityManagementMapper.insertGcQualityManagement(gcQualityManagement);
    }

    /**
     * 修改质量管理
     *
     * @param gcQualityManagement 质量管理
     * @return 结果
     */
    @Override
    public int updateGcQualityManagement(GcQualityManagement gcQualityManagement) {
        gcQualityManagement.setCreater(SecurityUtils.getNickName());
        gcQualityManagement.setCreateTime(DateUtils.getNowDate());
        // 保存附件
        businessFileService.saveBatchCommon(gcQualityManagement.getDocumentType(), String.valueOf(gcQualityManagement.getId()), gcQualityManagement.getFileList());
        return gcQualityManagementMapper.updateGcQualityManagement(gcQualityManagement);
    }

    /**
     * 批量删除质量管理
     *
     * @param ids 需要删除的质量管理主键
     * @return 结果
     */
    @Override
    public int deleteGcQualityManagementByIds(Long[] ids) {
        return gcQualityManagementMapper.deleteGcQualityManagementByIds(ids);
    }

    /**
     * 删除质量管理信息
     *
     * @param id 质量管理主键
     * @return 结果
     */
    @Override
    public int deleteGcQualityManagementById(Long id) {
        return gcQualityManagementMapper.deleteGcQualityManagementById(id);
    }
}
